package com.jacen.fzxs.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.jacen.fzxs.dao.IWallDao;
import com.jacen.fzxs.entity.WallEntity;
import com.jacen.fzxs.util.DateUtils;
import com.jacen.fzxs.util.SqlCondition;
import com.jacen.fzxs.util.Utils;


/**
 * 墙体Dao
 * @author Jacen
 * 2014年9月3日 下午2:30:40
 */
@Repository
public class WallDao extends BaseDao<WallEntity> implements IWallDao {
	
	public List<Map<String, Object>> getPage(int beginRow, int endRow ,Map<String, ? extends Object> params) {
		SqlCondition cond = getPageCondition(params);
		String sql = "SELECT w.* ,rw.cname,rw.trade,rw.renttime,rw.endtime,i.itemvalue tradename,i2.itemvalue lvname FROM t_wall w  "
				+ " LEFT JOIN r_wallcustomer rw  ON w.id = rw.wid "
				+ " LEFT JOIN t_dictitem i on rw.trade = i.itemname and i.groupname = 'trade'"
				+ " LEFT JOIN t_dictitem i2 on w.lv = i2.itemname and i2.groupname = 'level'"
				+ " where 1=1 %s order by w.create_time desc";
		sql = String.format(sql, cond.getCondition());
		sql = SqlCondition.pageSql(sql, beginRow, endRow);
		return getJdbcTemplate().queryForList(sql,cond.getParams().toArray());
	}
	
	public int getCount(Map<String, ? extends Object> paramMap) throws SQLException {
		SqlCondition cond = getPageCondition(paramMap);
		String sql = "select count(1) from t_wall w LEFT JOIN r_wallcustomer rw  ON w.id = rw.wid where 1=1 %s";
		sql = String.format(sql, cond.getCondition());
		return Long.valueOf(executeForLong(sql, cond.getParams().toArray())).intValue() ;
	}
	
	
	private SqlCondition getPageCondition(
			Map<String, ? extends Object> mapParams) {
		SqlCondition cond = new SqlCondition();
		if (Utils.paramIsNotEmpty(mapParams.get("cname")))
			cond.and().like("cname", mapParams.get("cname"));
		if (Utils.paramIsNotEmpty(mapParams.get("area")))
			cond.and().equal("area", mapParams.get("area"));
		if (Utils.paramIsNotEmpty(mapParams.get("price")))
			cond.and().equal("price", mapParams.get("price"));
		if (Utils.paramIsNotEmpty(mapParams.get("lv")))
			cond.and().equal("lv", mapParams.get("lv"));
		if (Utils.paramIsNotEmpty(mapParams.get("status")))
			cond.and().equal("w.status", mapParams.get("status"));
		if (Utils.paramIsNotEmpty(mapParams.get("trade")))
			cond.and().equal("rw.trade", mapParams.get("trade"));
		if (Utils.paramIsNotEmpty(mapParams.get("county")))
			cond.and().equal("county", mapParams.get("county"));
		if (Utils.paramIsNotEmpty(mapParams.get("city")))
			cond.and().equal("city", mapParams.get("city"));
		if (Utils.paramIsNotEmpty(mapParams.get("endTime")))
			cond.and().le("endtime", DateUtils.str2Date(mapParams.get("endTime").toString(), "yyyy-MM-dd"));
		
		if (Utils.paramIsNotEmpty(mapParams.get("username"))){
			cond.and().like("username", mapParams.get("username"));
		}
		if (Utils.paramIsNotEmpty(mapParams.get("tel"))){
			cond.and().like("tel", mapParams.get("tel"));
		}
		if (Utils.paramIsNotEmpty(mapParams.get("address"))){
			cond.and().like("address", mapParams.get("address"));
		}
		if (Utils.paramIsNotEmpty(mapParams.get("ids")))
			cond.and().in("w.id", mapParams.get("ids").toString().split(","));
		return cond;
	}

	@Override
	public List<Map<String, Object>> getList(Map<String, String> params) {
		SqlCondition cond = getPageCondition(params);
		String sql = "SELECT w.* ,rw.cname,rw.trade,rw.renttime,rw.endtime,i.itemvalue tradename,i2.itemvalue lvname FROM t_wall w  "
				+ " LEFT JOIN r_wallcustomer rw  ON w.id = rw.wid "
				+ " LEFT JOIN t_dictitem i on rw.trade = i.itemname and i.groupname = 'trade'"
				+ " LEFT JOIN t_dictitem i2 on w.lv = i2.itemname and i2.groupname = 'level'"
				+ " where 1=1 %s ";
		sql = String.format(sql, cond.getCondition());
		return getJdbcTemplate().queryForList(sql,cond.getParams().toArray());
	}

	@Override
	public WallEntity getEntity(String username, String tel) {
		String sql = "select * from t_wall t where t.username = '%s' and t.tel = '%s'";
		sql = String.format(sql, username,tel);
		return executeForObject(sql);
	}

	public List<WallEntity> getEntityList(Map<String, String> params) {
		SqlCondition cond = getPageCondition(params);
		String sql = "select * from t_wall t where 1=1 %s";
		sql = String.format(sql, cond.getCondition());
		return executeForList(sql,cond.getParams().toArray());
	}

	@Override
	public List<WallEntity> getListPage(int beginRow, int endRow,
			Map<String, String> params) {
		SqlCondition cond = getPageCondition(params);
		String sql = "SELECT * from t_wall w where 1=1 %s order by w.username";
		sql = String.format(sql, cond.getCondition());
		sql = SqlCondition.pageSql(sql, beginRow, endRow);
		return executeForList(sql, cond.getParams().toArray());
	}

	@Override
	public int getListCount(Map<String, String> params) {
		SqlCondition cond = getPageCondition(params);
		String sql = "select count(1) from t_wall w  where 1=1 %s";
		sql = String.format(sql, cond.getCondition());
		return Long.valueOf(executeForLong(sql, cond.getParams().toArray())).intValue() ;
	}


}
